<?php
class ArticleModel{

	protected $_name = 'eway_article';
	protected $_dbAdapter;

	function ArticleModel(){
		$this->_dbAdapter = Zend_Registry::get(Constant::DB_ADAPTER);
	}
	/** Dung cho trang chu */
	public function getAllArticleForView($items){		
		$sql = "SELECT * FROM ".$this->_name." WHERE art_published=1 ORDER BY art_published_date DESC LIMIT $items";
		$result = $this->_dbAdapter->fetchAll($sql);
		return $result;
	}
	
	public function insertArticle($article=array()){
		$this->_dbAdapter->insert($this->_name, $article);
	}

	public function updateArticle($article = array(), $article_id){
		$where = "art_id=".$article_id;
		$this->_dbAdapter->update($this->_name, $article, $where);
	}

	public function deleteArticle($article_id){
		$where = "art_id=".$article_id;
		$this->_dbAdapter->delete($this->_name, $where);
	}

	function getTotalRecords($published){
		$sql = "SELECT COUNT(*) FROM ".$this->_name." WHERE art_published=?";
		$result = $this->_dbAdapter->fetchCol($sql, $published);
		return $result[0];
	}

	public function getAllArticle($published, $currentPages, $recordPerPage){
		$offset = ($currentPages - 1) * $recordPerPage;
		$sql = "SELECT * FROM ".$this->_name." WHERE art_published=? ORDER BY art_published_date DESC LIMIT $offset,$recordPerPage";
		$result = $this->_dbAdapter->fetchAll($sql, $published);
		return $result;
	}
	
	public function getArticleById($article_id){
		$sql = "SELECT * FROM ".$this->_name." WHERE art_id=?";
		$result = $this->_dbAdapter->fetchRow($sql, $article_id);
		return $result;
	}

}